#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        if (prices.empty())
            return 0;
        int max_profit = 0;
        int buy_price, sell_price;
        for (int i = 0; i < prices.size() - 1; i++) {
            if (prices[i] < prices[i + 1]) {
                buy_price = prices[i];
                sell_price = prices[i + 1];
                for (int j = i + 2; j < prices.size(); j++) {
                    if (prices[j] < prices[j - 1]) {
                        sell_price = prices[j - 1];
                        i = j - 1;
                        break;
                    }
                }
                max_profit += sell_price - buy_price;
            }
        }
        return max_profit;
    }
    /*better way
    int maxProfit(vector<int>& prices)
    {
        int max_profit = 0;
        for (int i = 1; i < prices.size(); i++) {
            if (prices[i] > prices[i - 1])
                max_profit += prices[i] - prices[i - 1];
        }
        return max_profit;
    }
    */
};

int main(int argc, char const* argv[])
{
    Solution temp;
    int a[6] = { 7, 1, 5, 3, 6, 4 };
    vector<int> nums(a, a + 6);
    temp.maxProfit(nums);
    return 0;
}